﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Public.ResourceBase.UcControl
{
    public class TextBoxExtend : TextBox
    {
        public TextBoxExtend()
        {
            //这句话就是告诉应用程序可以在Themes/Generic文件夹下找到样式，否则，另一个程序集使用则需要添加类似如何的
            //DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxExtend), new FrameworkPropertyMetadata(default(TextBoxExtend)));
            Style = Application.Current.TryFindResource("TextBoxExtendStyle") as Style;

            //init
            this.ButtonContent = string.Empty;
        }

        #region Property

        public string WaterMark
        {
            get { return (string)GetValue(WaterMarkProperty); }
            set { SetValue(WaterMarkProperty, value); }
        }
        public static readonly DependencyProperty WaterMarkProperty =
            DependencyProperty.Register("WaterMark", typeof(string), typeof(TextBoxExtend), new PropertyMetadata(null));

        public int WaterMarkFontSize
        {
            get { return (int)GetValue(WaterMarkFontSizeProperty); }
            set { SetValue(WaterMarkFontSizeProperty, value); }
        }
        public static readonly DependencyProperty WaterMarkFontSizeProperty =
            DependencyProperty.Register("WaterMarkFontSize", typeof(int), typeof(TextBoxExtend), new PropertyMetadata(10));

        public Brush Icon
        {
            get { return (Brush)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }
        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(Brush), typeof(TextBoxExtend), new PropertyMetadata(null));

        public string ButtonContent
        {
            get { return (string)GetValue(ButtonContentProperty); }
            set { SetValue(ButtonContentProperty, value); }
        }
        public static readonly DependencyProperty ButtonContentProperty =
            DependencyProperty.Register("ButtonContent", typeof(string), typeof(TextBoxExtend), new PropertyMetadata(string.Empty));

        public bool IsShowClear
        {
            get { return (bool)GetValue(IsShowClearProperty); }
            set { SetValue(IsShowClearProperty, value); }
        }
        public static readonly DependencyProperty IsShowClearProperty =
            DependencyProperty.Register("IsShowClear", typeof(bool), typeof(TextBoxExtend), new PropertyMetadata(false));

        #endregion


        #region Command

        public ICommand ExtendCommand
        {
            get { return (ICommand)GetValue(ExtendCommandProperty); }
            set { SetValue(ExtendCommandProperty, value); }
        }
        public static readonly DependencyProperty ExtendCommandProperty =
            DependencyProperty.Register("ExtendCommand", typeof(ICommand), typeof(TextBoxExtend), new PropertyMetadata(null));

        public object ExtendCommandParamater
        {
            get { return GetValue(ExtendCommandParamaterProperty); }
            set { SetValue(ExtendCommandParamaterProperty, value); }
        }
        public static readonly DependencyProperty ExtendCommandParamaterProperty =
            DependencyProperty.Register("ExtendCommandParamater", typeof(object), typeof(TextBoxExtend), new PropertyMetadata(null));

        #endregion


        #region Control and Name
        private Button _btnDelete;
        private string _btnDeleteName = "PART_Delete";
        #endregion


        #region Override
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _btnDelete = GetTemplateChild(_btnDeleteName) as Button;
            if (_btnDelete != null)
                _btnDelete.Click += _btnDelete_Click;
        }
        #endregion


        #region Event
        private void _btnDelete_Click(object sender, RoutedEventArgs e)
        {
            Clear();
        }
        #endregion
    }
}
